package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.CmfzBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class CmfzBannerController {
    @Autowired
    private CmfzBannerService bannerService;
    @RequestMapping("selectAllBanners")
    @ResponseBody
    @LogAnnotation(content = "查询所有轮播图信息",type = "查询")
    public Map selectAllBanners(Integer page,Integer limit){
        HashMap map = new HashMap();
        Page<CmfzBanner> bannerPage = bannerService.selectAllBanners(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",bannerPage.getTotal());
        map.put("data",bannerPage.getRecords());
        return map;
    }
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file) throws IOException {
        HashMap map = new HashMap();
        //        //1.file 得到此时用户点击的图片
        //2.得到图片的名字 filename
        String filename = file.getOriginalFilename();
        //transferTo:将上传的文件复制到指定的文件夹下，名字以filename保存
        //要存放在文件夹下，则需要在创建file对象时构造保存路径和文件名
        file.transferTo(new File("C:\\img\\cmfzimg",filename));
        String bannerImageUrl = "cmfzimg/"+filename;
        System.out.println(bannerImageUrl);
        map.put("bannerImageUrl",bannerImageUrl);
        return map;
    }
    @RequestMapping("insertBanner")
    @ResponseBody
    @LogAnnotation(content = "添加轮播图",type = "添加")
    public Map insertBanner(CmfzBanner banner){
        HashMap map = new HashMap();
        Date utilDate = new Date();
        // long time = utilDate.getTime();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        //获取到的日期也可以被format成我们需要的格式
        //SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        banner.setBannerDate(sqlDate);
        //System.out.println(banner);
        try {
            bannerService.insertBanner(banner);
            map.put("flag",true);
        }catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("selectOneBanner")
    @LogAnnotation(content = "查询一个轮播图",type = "查询")
    public CmfzBanner selectOneBanner(Integer bannerId){
        CmfzBanner banner = bannerService.selectOneBannerById(bannerId);
        return banner;
    }
    @ResponseBody
    @RequestMapping("updateBanner")
    @LogAnnotation(content = "更新轮播图",type = "更新")
    public Map updateBanner(CmfzBanner banner){
        System.out.println(banner);
        HashMap map = new HashMap();
        try {
            bannerService.updateBanner(banner);
            map.put("flag",true);
        }catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("deleteBanner")
    @LogAnnotation(content = "删除轮播图",type = "删除")
    public Map deleteBanner(Integer bannerId){
        HashMap map = new HashMap();
        try {
            bannerService.deleteBannerById(bannerId);
            map.put("flag",true);
        }catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
}
